<?php

/**
 * Class for work with database table "keys"
 * 
 * @version 2013-01-25
 * @package Db model
 * @subpackage accounts
 * @uses Db_Table
 * @category Db model
 */

class KeysModel extends PDO{

	/**
     * __construct
     * This construct function connects to the table "keys".
     *
     * @version 2012-10-10
     * @uses parent::__construct('keys')
     * @return object
     */
	function __construct()
	{
		parent::__construct('sqlite:'._cfg('file.path').'sqlite/protectedkey.sqlite3');
		//$sqLite = new PDO();
		$this->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
		$this->exec("CREATE TABLE IF NOT EXISTS keys (uuid TEXT,key TEXT)");
	}
	
	 /**
     * addKey
     * This function add the key about a member and inserts the data into the table "sqlite:keys"
     *
     * @version 2013-01-25
     * @param string $uuid    
     * @return void
     * @access public
     */
	function addKey($uuid){
		if(!$this->getKey($uuid)){
			$insert = "INSERT INTO keys (uuid, key) VALUES ('".sha1($uuid)."','".sha1($uuid.time()._cfg('key1'))."')";
			$this->exec($insert);
		}
	}
	
	 /**
     * getKey
     * This function gets the key about a member in the table "sqlite:keys".
     *
     * @version 2013-01-25
     * @param string $uuid     
     * @return string
     * @access public
     */
	function getKey($uuid){
		$request = $this->prepare("SELECT key FROM keys WHERE uuid ='".sha1($uuid)."'");
		$request->execute();
		$result = $request->fetch(PDO::FETCH_ASSOC);
		return $result['key'];
	}	 
	
	
	/**
     * deleteKey
     * This function delete key about a member in the table "sqlite:keys".
     *
     * @version 2013-01-25
     * @param string $uuid     
     * @return string
     * @access public
     */
	function delKey($uuid){
		$request = $this->prepare("DELETE FROM keys WHERE uuid ='".sha1($uuid)."'");
		return $request->execute();
	}
}    
